package com.gsafety.wellsfort.controller.security;

import com.gsafety.wellsfort.common.util.ApiResponse;
import com.gsafety.wellsfort.common.util.ResultCode;
import com.gsafety.wellsfort.domain.dto.ids.KnowledgeQueryDto;
import com.gsafety.wellsfort.domain.dto.ids.RoleIdDTO;
import com.gsafety.wellsfort.domain.dto.ids.RoleQueryDto;
import com.gsafety.wellsfort.domain.form.RoleForm;
import com.gsafety.wellsfort.domain.form.Tree;
import com.gsafety.wellsfort.domain.model.security.Role;
import com.gsafety.wellsfort.domain.vo.KnowledgeResultVO;
import com.gsafety.wellsfort.domain.vo.QueryVO;
import com.gsafety.wellsfort.domain.vo.security.RoleVO;
import com.gsafety.wellsfort.service.security.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(description = "角色接口")
@RestController
@Slf4j
@RequestMapping("/api/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;


    /**
     * 角色列表:
     * (1)总平台管理员组查看所有角色
     * (2)学校平台管理员查看学校平台以及学校教师平台角色
     * (3)学校教师平台角色,查看学校教师平台角色
     *
     * @return
     */
    @ApiOperation(value = "角色列表")
    @PostMapping("/list")
    public ApiResponse<List<Role>> list(@RequestBody QueryVO queryVO) {
        return roleService.pageQuery(queryVO);
    }


    /**
     * 菜单列表
     *
     * @return
     */
    @ApiOperation(value = "菜单列表,根据角色组动态获取角色")
    @PostMapping("/getAllByGroup")
    public ApiResponse<List<Role>> getAllByGroup(@RequestBody RoleQueryDto roleQueryDto) {
        if (roleQueryDto == null) {
            return ApiResponse.error(ResultCode.INVALID_PARAM);
        }
        return roleService.getAllByGroup(roleQueryDto.getSchoolId());
    }


    /**
     * 角色添加
     *
     * @return
     */
    @ApiOperation(value = "添加角色")
    @PostMapping("/add")
    public ApiResponse add(@RequestBody RoleVO roleVO) {
        if (roleService.addRole(roleVO)) {
            return ApiResponse.success();
        } else {
            return ApiResponse.error();
        }
    }

    /**
     * 设置权限
     *
     * @return
     */
    @ApiOperation(value = "设置权限")
    @PostMapping("/setPermission")
    public ApiResponse setPermission(@RequestBody RoleForm roleForm) {
        if (roleService.setPermission(roleForm)) {
            return ApiResponse.success();
        } else {
            return ApiResponse.error();
        }
    }

    /**
     * 获取权限
     *
     * @return
     */
    @ApiOperation(value = "获取权限")
    @PostMapping("/getPermission")
    public ApiResponse<List<Tree>> getPermission(@RequestBody RoleIdDTO idDTO) {
        return roleService.getPermission(idDTO.getRoleId());
    }

    /**
     * 角色更新
     *
     * @return
     */
    @ApiOperation(value = "更新角色")
    @PostMapping("/update")
    public ApiResponse update(@RequestBody RoleVO roleVO) {
        if (roleService.updateRole(roleVO)) {
            return ApiResponse.success();
        } else {
            return ApiResponse.error();
        }
    }

    /**
     * 获取权限
     *
     * @return
     */
    @ApiOperation(value = "获取权限")
    @PostMapping("/getKnowledgePermission")
    public ApiResponse<List<KnowledgeResultVO>> getKnowledgePermission(@RequestBody KnowledgeQueryDto knowledgeQueryDto) {
        return roleService.getKnowledgePermission(knowledgeQueryDto);
    }
}
